<?php
$accountID = 1;

$mysqlConnection = mysql_connect("localhost", "user", 12345678) or die ("Es ist ein Fehler aufgetreten  Fehlercode: 100");
mysql_select_db("ow") or die ("Es ist ein Fehler aufgetreten  Fehlercode: 101");
?>

<!DOCTYPE>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="messages.css">
	</head>
	<body>
		<div id="messages">
		<?php
			if(isset($_GET["show"]))
			{
				$show = $_GET["show"];
				
				$message = mysql_query("SELECT * FROM Messages WHERE messageID = '$show'");
				
				if($message == false)
					die("Nachricht existiert nicht");
					
				
				$row = mysql_fetch_object($message);
				
				$sender = mysql_fetch_object( mysql_query("SELECT name FROM Account WHERE accountID = '$row->sender'") )->name;
				
				
				$eins = 1;
				//als gelesen markieren
				$update = mysql_query("UPDATE Messages Set read123 = '$eins' WHERE messageID = '$show'");
				if($update == false)
					die(mysql_error());
				
				?>
				
				<div class="message">
					<div class="header">
						<span class="sender"><?php echo $sender; ?></span> <span class="subject"><?php echo $row->subject; ?></span> <span class="date"><?php echo $row->sendDate; ?></span>
					</div>
					<div class="text">
						<?php echo $row->message ?>
					</div>
				</div>
				
				<?php
			}
			else
			{			
				$messages = mysql_query("SELECT * FROM Messages WHERE receiver = '$accountID'");
				while($row = mysql_fetch_object($messages))
				{
					if($row->read123 == 0)
						$read = "unread";
					else
						$read = "read";
					
					$sender = mysql_fetch_object( mysql_query("SELECT name FROM Account WHERE accountID = '$row->sender'") )->name;
					?>
					
					<a href="messages.ui.inc.php?show=<?php echo $row->messageID; ?>" class="head <?php echo $read; ?>">
							<span class="sender"><?php echo $sender; ?></span> <span class="subject"><?php echo $row->subject; ?></span> <span class="date"><?php echo $row->sendDate; ?></span>
					</a><br />
					
					<?php
				}
			}
		?>			
		</div>
		<?php 
			if(isset($_GET["show"]))
			{
				?><h3>Antworten:</h3><?php
			}
			else
			{
				?><h3>Neue Nachricht:</h3><?php
			}
		?>
		
		<form action="messages.pr.inc.php" method="POST"></td>
			<?php 
				if(isset($_GET["show"]))
				{
					?>
					<input type="hidden" name="reply" value="<?php echo $show; ?>" />
					<input type="hidden" name="receiver" value="<?php echo $sender; ?>" />
					<?php
				}
			?>
			<table>
				<?php 
					if(!isset($_GET["show"]))
					{
						?>
						<tr>
							<td>An:</td><td><input type="text" name="receiver" /></td>
						<tr>
						<?php
					}
				?>
				</tr>
					<td>Betreff:</td><td><input type="text" name="subject" /></td>
				<tr>
				</tr>
					<td>Text:</td><td><textarea name="message" cols="50" rows="10"></textarea></td>
				</tr>
				<tr>
					<td><input type="submit" value="senden"/></td>
				</tr>
			</table>
		</form>
	</body>
</html>